package cn.tedu.hotel.service.impl;

import cn.tedu.hotel.ex.ServiceException;
import cn.tedu.hotel.mapper.BillMapper;
import cn.tedu.hotel.mapper.CategoryMapper;
import cn.tedu.hotel.mapper.CustomerMapper;
import cn.tedu.hotel.mapper.RoomMapper;
import cn.tedu.hotel.pojo.dto.BillAddNewDTO;
import cn.tedu.hotel.pojo.entity.Bill;
import cn.tedu.hotel.pojo.entity.Category;
import cn.tedu.hotel.pojo.entity.Room;
import cn.tedu.hotel.pojo.vo.BillLIstItemVO;
import cn.tedu.hotel.pojo.vo.CategoryStandardVO;
import cn.tedu.hotel.pojo.vo.CustomerStandardVO;
import cn.tedu.hotel.pojo.vo.RoomStandardVO;
import cn.tedu.hotel.service.IBillService;
import cn.tedu.hotel.web.JsonPage;
import cn.tedu.hotel.web.ServiceCode;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 *  订单业务的实现类
 */
@Slf4j
@Service
public class BillServiceImpl implements IBillService, ServiceCode {

    @Autowired
    private BillMapper billMapper;
    @Autowired
    private RoomMapper roomMapper;
    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private CategoryMapper categoryMapper;

    public BillServiceImpl(){
        log.debug("开始创建业务对象：BillServiceImpl");
    }


    @Override
    public void addNew(BillAddNewDTO billAddNewDTO) {
        log.debug("开始处理【新增订单】业务：{}",billAddNewDTO);

        // 获取房间id，客户id
        Long roomId = billAddNewDTO.getRoomId();
        Long customerId = billAddNewDTO.getCustomerId();

        // 根据房间id查询房间状态是否是空闲
        {
            RoomStandardVO standardById = roomMapper.getStandardById(roomId);
            // 获取房间状态
            Integer state = standardById.getState();
            // 判断房间状态是否是空闲 0:空闲
            if (state != 0) {
                String message = "添加失败，房间已不存在";
                log.warn(message);
                throw new ServiceException(ERR_NOT_FOUND, message);
            }
        }

        //  检查客户是否存在
        {
            CustomerStandardVO standardById = customerMapper.getStandardById(customerId);
            if (standardById == null){
                String message = "新增失败，客户不存在";
                log.warn(message);
                throw new ServiceException(ERR_NOT_FOUND,message);
            }
        }

        // 获取当前时间
        LocalDateTime now = LocalDateTime.now();

        // 创建bill对象 并赋值
        Bill bill = new Bill();
        BeanUtils.copyProperties(billAddNewDTO,bill);
        bill.setGmtCreate(now);

        // 执行新增订单
        {
            int rows = billMapper.insert(bill);
            if (rows != 1) {
                String message = "新增失败，服务器忙，请稍后再试";
                log.warn(message);
                throw new ServiceException(ERR_INSERT, message);
            }
        }

        // 修改房间状态
        {
            Room room = new Room();
            room.setId(roomId);
            room.setState(1);
            int rows = roomMapper.update(room);
            if (rows != 1){
                String message = "修改失败，服务器忙，请稍后再试";
                log.warn(message);
                throw new ServiceException(ERR_UPDATE, message);
            }
        }

        // 修改房间剩余数量
        {
            Category category = new Category();
            CategoryStandardVO categoryByRoomId = categoryMapper.getCategoryByRoomId(roomId);
            if (categoryByRoomId == null){
                String message = "房间类型不存在，无法修改";
                log.debug(message);
                throw new ServiceException(ERR_NOT_FOUND, message);
            }

            Long categoryId = categoryByRoomId.getId();
            category.setId(categoryId);
            Integer surplus = categoryByRoomId.getSurplus()-1;
            category.setSurplus(surplus);
            categoryMapper.update(category);
        }

    }

    @Override
    public JsonPage<BillLIstItemVO> getAllBillsByPage(Integer page, Integer pageSize) {
        PageHelper.startPage(page,pageSize);

        List<BillLIstItemVO> list = billMapper.listByBill();

        return JsonPage.restPage(new PageInfo<>(list));
    }


}
